import { Component, EventEmitter, Input, OnChanges, Output, SimpleChanges } from "@angular/core";
import { DateTimeService } from "@app/shared/common/timing/date-time.service";
import { DateTimeHelper } from "@shared/helpers/DateTimeHelper";
import { DateTime } from "luxon";


@Component({
    selector: 'product-time-ctl',
    templateUrl: './product-time-ctl.component.html'
})
export class ProductTimeCtlComponent implements OnChanges{

    @Input() establishmentDate:string
    @Input() dueDate:string
    @Input() timeLimit:string

    @Input() disableEstablishmentDate:boolean
    @Input() disableDueData:boolean
    @Input() disableTimeLimit:boolean

    @Output() establishmentDateChange = new EventEmitter<string>();
    @Output() dueDateChange = new EventEmitter<string>();
    @Output() timeLimitChange = new EventEmitter<string>();

    startTime?:DateTime
    endTime?:DateTime


    constructor(private _dateTimeService:DateTimeService){

    }
    ngOnChanges(changes: SimpleChanges): void {

        if (changes['establishmentDate']){
            if (changes['establishmentDate'].currentValue != changes['establishmentDate'].previousValue){
                if (this.establishmentDate){
                    this.startTime =  DateTimeHelper.FromUnixTimestampByMilliseconds(Number(this.establishmentDate));
                }
            }
        }

        if (changes['dueDate']){
            if (changes['dueDate'].currentValue != changes['dueDate'].previousValue){
                if (this.dueDate){
                    this.endTime =  DateTimeHelper.FromUnixTimestampByMilliseconds(Number(this.dueDate));
                }
            }
        }
    }

    dateTimeChange(){
        if (!this.startTime) {
            this.establishmentDate = '';
            this.establishmentDateChange.emit(this.establishmentDate);
            return;
        }
        let newTime= DateTimeHelper.ToUnixTimeByMilliseconds(this.startTime).toString();
        this.establishmentDateChange.emit(newTime);
        this.establishmentDate = newTime;
        this.changeData();
    }

    dateTimeChange1(){
        if (!this.endTime) {
            this.dueDate = '';
            this.dueDateChange.emit(this.dueDate);
            return;
        }
        let newTime= DateTimeHelper.ToUnixTimeByMilliseconds(this.endTime).toString();
        this.dueDateChange.emit(newTime);
        this.dueDate = newTime;
        this.changeData();
    }

    changeData(){
        if (!this.getTimeNum(this.establishmentDate) || !this.getTimeNum(this.dueDate)) {
            this.timeLimit = "0";

        }
        else {
            let dur = this.getTimeNum(this.dueDate) - this.getTimeNum(this.establishmentDate);
            let t = dur / 1000 / 86400;
            this.timeLimit = t.toString();
        }
        this.timeLimitChange.emit(this.timeLimit);

    }


    private getTimeNum(time:string): number{
        try {
            return Number.parseInt(time);
        } catch (error) {
            return 0;
        }
    }

}